package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.PageResult;
import com.mall.common.R;
import com.mall.entity.SmoothMallProduct;
import com.mall.service.SmoothMallProductService;
import io.minio.GetObjectArgs;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

@RestController
public class SmoothMallProductController {
    @Autowired
    SmoothMallProductService smoothMallProductService;
    @GetMapping("/admin/product/list/{pageNum}/{pageSize}")
    @PreAuthorize("@maxAnger.hasAuthority('admin')")
    public PageResult<SmoothMallProduct> getlist(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Page pageInfo = new Page(pageNum, pageSize);
        Page page = smoothMallProductService.page(pageInfo);
        PageResult<SmoothMallProduct> smoothMallProductPageResult = new PageResult<>();
        smoothMallProductPageResult.setRows(pageInfo.getRecords());
        smoothMallProductPageResult.setTotalCount((int) pageInfo.getTotal());
        return smoothMallProductPageResult;
    }
    //添加商品
    @PostMapping("/admin/product/add")
    public R add(@RequestBody SmoothMallProduct smoothMallProduct){
        boolean save = smoothMallProductService.save(smoothMallProduct);
        if (save){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    //更新商品
    @PostMapping("/admin/product/update")
    public R update(@RequestBody SmoothMallProduct smoothMallProduct){
        boolean save = smoothMallProductService.updateById(smoothMallProduct);
        if (save){
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }
    //删除商品
    @DeleteMapping("/admin/product/delete/{id}")
    public R delete(@PathVariable Integer id){
        boolean b = smoothMallProductService.removeById(id);
        if (b){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    //上传图片
    @PostMapping("/admin/upload/file")
    public String upload(@RequestPart("filedata")MultipartFile filedata) throws IOException {
        File mallFile = File.createTempFile("mall", ".jpg");
        filedata.transferTo(mallFile);
        String s = "http://file.51xuecheng.cn/"+smoothMallProductService.uploadMall(mallFile);
        //返回路径图片的访问请求路径
        return s;
    }
    //批量上下架商品
    @PostMapping("/admin/product/batchUpdateSellStatus/{sellStates}")
    public R deleteByIds(@PathVariable Integer sellStates,@RequestParam List<Long> ids) {
            List<SmoothMallProduct> list = smoothMallProductService
                    .list(new LambdaQueryWrapper<SmoothMallProduct>()
                            .in(SmoothMallProduct::getId,ids));
            if (list==null){
                return R.error("不存在该集合");
            }
        if (sellStates==1){
            list.stream().forEach(item->{
                item.setStatus(1);
                smoothMallProductService.updateById(item);
            });
        }else {
            list.stream().forEach(item->{
                item.setStatus(0);
                smoothMallProductService.updateById(item);
            });
        }
        return R.success("yes");
    }
    //查看详情
    @PostMapping("/product/detail/{id}")
    public R getDetail(@PathVariable Integer id){
        SmoothMallProduct mallProduct = smoothMallProductService.getByIdWithPassThrough(id);
        if (mallProduct==null){
            return R.error("商品不存在！");
        }
        return R.success(mallProduct);
    }
    //前台查看列表
    @GetMapping("/admin/productst/{pageNum}/{pageSize}/{categoryId}")
    public R list(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@RequestParam String keyword,@RequestParam String orderBy) {
        Page<SmoothMallProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmoothMallProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(keyword != null, SmoothMallProduct::getName, keyword);
        if (orderBy.equals("price desc")){
            queryWrapper.orderByDesc(SmoothMallProduct::getPrice);
        }else {
            queryWrapper.orderByAsc(SmoothMallProduct::getPrice);
        }
        smoothMallProductService.page(page, queryWrapper);
        return R.success(page);
    }
    @GetMapping("/product/mkdir/list")
    public R<Page> list(Integer pageNum,Integer pageSize,String name){
        Page<SmoothMallProduct> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmoothMallProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,SmoothMallProduct::getName,name);
        queryWrapper.orderByDesc(SmoothMallProduct::getPrice);
        smoothMallProductService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
}
